package cn.tedu.charging.order.mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * mqtt消息发送者
 * 订单服务 发送 消息 给 EMQX
 */
@Component
public class MqttProducer {

    private static final Logger log = LoggerFactory.getLogger(MqttProducer.class);

    @Autowired
    private MqttClient mqttClient;

    /**
     * 通过MQTTClient 发送消息到EMQX
     * @param topic
     * @param message
     * @throws Exception
     */
    public Boolean send(String topic,String message) {
        Boolean success = false;
        try {
            //topic 是啥?todo
            //getBytes 是字符串的方法 获取byte[]
            mqttClient.publish(topic,
                    message.getBytes(), 0, false);
            success = true;
        }catch (Exception e) {
            log.error("给EMQX发送消息失败,topic:{},消息:{}",topic,message,e);
        }
        return success;
    }
}
